<?php
//Class coding by tutrongvnn
//Help
/*
//css
	.font {
	FONT-SIZE: 8pt;
	COLOR: #333333;
	FONT-FAMILY: Tahoma;
	}
	.font a {color:#669900; font-weight:bold;}
	.font a:hover {color:#99cc00; font-weight:bold;}
	
	a:visited.page {border:1px solid #C0C0C0; text-decoration:none; color: #333333; padding-left:3px; padding-right:3px; padding-top:0px; padding-bottom:1px; background-color:#F3F3F3}
	a:link.page {border:1px solid #C0C0C0; text-decoration:none; color: #333333; padding-left:3px; padding-right:3px; padding-top:0px; padding-bottom:1px; background-color:#F3F3F3}
	a:hover.page {border:1px solid #C0C0C0; text-decoration:none; color: #333333; padding-left:3px; padding-right:3px; padding-top:0px; padding-bottom:1px; background-color:#C0E95C}
	
	.page {
		border:1px solid #C0C0C0;
		text-decoration:none;
		color: #333333;
		padding-left:3px;
		padding-right:3px;
		padding-top:0px;
		padding-bottom:1px;
		background-color:#C0E95C
	}
			//paging	
					$sql = "select.....";
					$pp=10; 
					$parr=array();
					$parr=paging($sql,$pp);
					$page=$parr[0];
					$limitvalue=$parr[1];
					$numofpages=$parr[2];
					$sql.=" LIMIT $limitvalue, $pp";
			//End Paging
			
	//show page
	 
	 <p align="center" class="font">
	 <?php echo page_div( "?page=news_cat&id=$id&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	 </p>

*/


	function page_div($link, $offset, $numofpages, $page) {
	$numofpages = ceil ( $numofpages );
	$pagesstart = ceil ( $page - $offset );
	$pagesend = ceil ( $page + $offset );
	if ($page != "1" && ceil ( $numofpages ) != "0") {
		$str=str_replace ( '%d_pg', ceil ( $page - 1 ), "<a class=\"page\" href=" . $link . ">Trước</a>&nbsp;&nbsp;" );
	}
	for($i = 1; $i <= $numofpages; $i ++) {
		if ($pagesstart <= $i && $pagesend >= $i) {
			if ($i == $page) {
				$str=$str."$i&nbsp;&nbsp;";
			} else {
				$str=$str.str_replace ( '%d_pg', "$i", '<a class="page" href="' . $link . '">' . $i . '</a>&nbsp;&nbsp;' );
			}
		}
	}
	if (ceil ( $numofpages ) == "0") {
		$str=$str."$i";
	}
	if ($page != ceil ( $numofpages ) && ceil ( $numofpages ) != "0") {
		$str=$str.str_replace ( '%d_pg', ceil ( $page + 1 ), "<a class=\"page\" href=" . $link . ">Tiếp</a>" );
	}
	return $str;
}

//Ham phan trang

function paging($sql,$st){
		//Phan trang 
		$pp=$st;  //so ban ghi tren 1 trang
		$p_now = intval($_GET ['p']);
		$result=mysql_query($sql) or die(mysql_error());
			$total=mysql_num_rows($result);  //lay tong so dong
			$numofpages=$total/$pp;
				
			if ($p_now<=0) {
				$page = 1;
			} else {
				if($p_now <= ceil($numofpages))
					$page = $p_now;
				else
					$page = 1;
			}
				$limitvalue = $page * $pp - ($pp);
		$parr=array();
		$parr[0]=$page;
		$parr[1]=$limitvalue;
		$parr[2]=$numofpages;
	return $parr; 
}
?> 

